<?php

namespace ziggle\demo\tests;

use PHPUnit\Framework\TestCase;
use ziggle\demo\params\GoodsInfoVo;
use ziggle\demo\params\PayApplyReqVo;
use ziggle\demo\params\PaymentApplyReqVo;
use ziggle\demo\params\PaymentQueryReqVo;
use ziggle\demo\params\PayQueryReqVo;
use ziggle\demo\params\RefundApplyReqVo;
use ziggle\demo\params\RefundDetailVo;
use ziggle\demo\params\RefundQueryReqVo;
use ziggle\demo\params\RegisterApplyReqVo;
use ziggle\demo\params\RegisterQueryReqVo;
use ziggle\demo\params\ShareInfoVo;
use ziggle\demo\params\ShareListVo;
use ziggle\demo\params\UploadDocumentRequest;
use ziggle\demo\util\Rsa;
use ziggle\demo\util\Sign;

require '..\src\cfg.php';
require '..\src\util\Http.php';

class App extends TestCase
{

    /**
     * 进件申请
     * @return void
     */
    public function test_apply()
    {
        $req = new RegisterApplyReqVo();
        $reqVO = '{
    "txn_seqno":"20873274993898100139509189968366",
    "notify_url":"http://192.168.110.28:8080/llpaytest/mpspNotifyReceive_test.htm",
    "extend_info":"",
    "subject_info":{
        "subject_type":"SUBJECT_TYPE_INDIVIDUAL",
        "business_license_info":{
            "license_copy":"211130162600010004",
            "license_number":"91330100MA27Y5E649",
            "merchant_name":"连连银加信息技术有限公司",
            "merchant_short_name":"连连银加"
        },
        "identity_info":{
            "id_doc_type":"IDCARD",
            "id_doc_phone":"L058RdAMAnKZKD5sGu6IUdQTlK5C3oldxjZPAlDwKEttPze50RSlmUejMIg7IVtF8jxWP121U1K9TSbBObaQAvGLncSI/StYSi3+N7JVWbshl07qgTmYNJHUsg64Z4TeYTuZSTzaXrpDG/4T0IxvjBgdF4ot2AwLwC4hrfxq+3M=",
            "id_card_info":{
                "id_card_copy":"211130162600010004",
                "id_card_national":"211130162600010004",
                "id_card_name":"ghGj65dUyqoT2nwFT4J2TJakmgOsDDlN4ZArEz9ituh9UaGUlWBtOkrG21znXH9AtViDNofNPq03C4ixhDyyEzapZ//DIT6xUZ/mOH5Q8phhiBZPl/Xc0i4c7U866SNwyB06w8STeE0gjwBxTmqYrw71mvctqXTCGE25OhbdPOQ=",
                "id_card_number":"ahsjWSCQ1d9TghZKXF3AJvRvHGLoFbfHENXKkSVrGAquZHcG7EvAhN/BXF6mQPeXFYvZfYBxNDODSAPabjYUdgDlhR7glVZvgmClLnsAcFVOJ1AqoJXWpV67NScH1Pu8nEQetk45p3nyWRmfGOjmil+O5x2B8s4K6YXh3ue98pQ=",
                "card_period_begin":"2019-01-05",
                "card_period_end":"2032-06-02"
            }
        },
        "ubo_info":[
            {
                "id_type":"IDCARD",
                "id_card_copy":"211130162600010004",
                "id_card_national":"211130162600010004",
                "id_doc_copy":"",
                "name":"I62CvtCqhQfejb58NNZ/Ow1iTX9sWD1QXnjNVcSCIx1812drcFIJRD1Piis9r5lrJ9I0Stpu0FJHIVw+aFuEr51jhR9BYlwiDbTT1uuIKEkYlOohhnGkHqauGmT5UWL4Y2QojHHEqxmB+xMHu5C6qG0nWHUoSOFPFRl0Rs2yri0=",
                "id_number":"Nke7tqgN4tHJeKtOzEyxs/c8D30z1ob1STSuK/+1Fl5ENnUAg5DjQn3PgaUfWydktAea8R13+8OmfhnG29XhHKIjRSwy3Vw9JCuQJ7evYkyacVWWnGPQ/kwHZDoqilKZb7tB9suiUOfs6YPJktL7UXLnr/Q/gbmbiA08iREKFm4=",
                "id_period_begin":"2019-12-01",
                "id_period_end":"2099-12-03"
            }
        ],
        "address_info":{
            "merchant_province":"110000",
            "merchant_city":"110100",
            "merchant_area":"110102",
            "merchant_address":"123434"
        }
    },
    "contact_info":{
        "contact_name":"Qv6+ZJsWlH3ueg9111zQrHE1ETNwvF+cLB5R/aNNnbwHVjhlseQFzxMbBc1gZHKH4YbnWTOdNrXZpfsFl1yWBx24HK4FWeWxI3ku0cvW99sH0XDPqJexXvkyZAAQvVTwXqH7rT2YyP/xJeZXhMlJzF935M7NP1Ub0Mer3Lunw9g=",
        "contact_phone":"Qv6+ZJsWlH3ueg9111zQrHE1ETNwvF+cLB5R/aNNnbwHVjhlseQFzxMbBc1gZHKH4YbnWTOdNrXZpfsFl1yWBx24HK4FWeWxI3ku0cvW99sH0XDPqJexXvkyZAAQvVTwXqH7rT2YyP/xJeZXhMlJzF935M7NP1Ub0Mer3Lunw9g=",
        "contact_email":"Y5GqOTl3Sd9E2fTirNVYS30jlP2W8dCThfVIwj8yoqE23ScX9U3Sx00hTNLtPr36QfL8X/kFNp6qOOspH+hRnPGfDinakuTzJ/VmFuRB6LOep7/HsV5rL0z4vMR0IA/N9mYAsgclRepHg6L+DBTk98YndvxuUJOEgvQXzcvmlyw="
    },
    "bank_account_info":{
        "bank_account_type":"BANK_ACCOUNT_CORPORATE",
        "account_name":"DfOsCmvSbKQgGTMKPStYc7kajT2X5xtEFUAGEtyb8KvudJjG3tmlp4ztfxnXffpqIdH38E/J4r4BHI+r/XWj13ukfysD7Bc45Stmx/FqhBnl4bAkp5tX9HM8BsyT19CMQMtTdw4Pazfjz+Xo/XKQ+rsvxNz+LY7UjzCwZHiI1NY=",
        "account_bank":"01020000",
        "bank_name":"中信银行",
        "account_number":"PUnkhCJckUgKEEmPNmKJG4OzB0HbQFvGpOwq8UPe+BSNoBiIF1gFPyvgMZH9ceCkq7SkfwGiUCD+2ic/jmWHkihvh/ThOgpQodansnxB9SX8yu7Z4xKfXkHdVHwDYG6+0Y1fSPiRUPmxkuYDNGQ6ebqfpbAtH4Mx60/mlNCCg2o="
    },
    "business_info":{
        "service_phone":"0758-1231334",
        "business_scope":"支付行业",
        "category_code":"1009",
        "business_scenario_desc":"支付行业领军企业",
        "sales_info":{
            "sales_scenes_type":[
                "SALES_SCENES_WEB",
                "SALES_SCENES_MP",
                "SALES_SCENES_APP"
            ],
            "mp_info":{
                "mp_appid":"928908892",
                "mp_sub_appid":"588444241"
            },
            "app_info":{
                "app_name":"微信app",
                "app_appid":"777774197",
                "app_sub_appid":"298604477"
            },
            "web_info":{
                "domain":"435845205",
                "web_appid":"591149208"
            }
        }
    },
    "product_info":{
        "wx_pay_conf":{
            "common_item":{
                "rule_type":"FIXED_AMOUNT",
                "rule_amount":"3.1"
            }
        },
        "ali_pay_conf":{
            "common_item":{
                "rule_type":"FIXED_RATE",
                "rule_percent":"3.01",
                "down_limit":"2.02"
            }
        },
        "immediate_pay_conf":{
            "credit_card_item":{
                "rule_type":"FIXED_AMOUNT",
                "rule_amount":"3.1",
                "down_limit":"3.02"
            },
            "debit_card_item":{
                "rule_type":"FIXED_AMOUNT",
                "rule_amount":"3.1",
                "down_limit":"3.02"
            },
            "public_card_item":{
                "rule_type":"FIXED_AMOUNT",
                "rule_amount":"3.1",
                "down_limit":"3.02"
            }
        }
    }
}';
        $resp = httpPost(TEST_URL . '/v1/customer/access/apply', $reqVO);
        Logger()->info($resp);
        $ret_code = (array)json_decode($resp, true)['ret_code'];
        $this->assertSame("0000", $ret_code[0]);

    }


    /**
     * 付款申请
     * @return void
     */
    public function test_payment()
    {
        $req = new PaymentApplyReqVo();
        $req->mch_id = MCH_ID;
        $req->txn_seqno = date("YmdHis");
        $req->txn_time = date("YmdHis");
        $req->order_amount = "0.1";
        $req->acct_name = Sign::enc("李三"); // 属性加密
        $req->order_info = "test order info ";
        $req->card_no = Sign::enc("6228480223312311474");  // 属性加密
        $req->memo = "memo";
        $req->flag_card = "PRIVATE_CASH";
        $req->risk_item = "{\"frms_ware_category\":\"2009\",\"user_info_mercht_userno\":\"12345\",\"user_info_bind_phone\":\"13812345678\",\"user_info_dt_register\":\"20141015165530\",\"user_info_full_name\":\"张三丰\",\"user_info_id_no\":\"3306821990012121221\",\"user_info_identify_type\":\"1\",\"user_info_identify_state\":\"1\",\"user_info_id_type\":\"0\",\"frms_client_chnl\":\"10\",\"frms_ip_addr\":\"60.191.76.226\"}";
        $req->notify_url = "http://localhost:9090";
        $req->bank_code = "99990001";
        $req->prcptcd = "102308001068";
        $req->city_code = "";
        $req->brabank_name = "中国农业银行";

        $resp = httpPost(TEST_URL . '/v1/ipay/cashout/payment/apply', json_encode($req));
        Logger()->info($resp);
        $ret_code = (array)json_decode($resp, true)['ret_code'];
        $this->assertSame("0000", $ret_code[0]);
    }

    /**
     * 退款申请
     * @return void
     */
    function test_refund()
    {
        $refundDetailVo = new RefundDetailVo();
        $refundDetailVo->refund_mch_id = "201608110000196145";
        $refundDetailVo->refund_amount = "0.1";

        $req = new RefundApplyReqVo();
        $req->mch_id = MCH_ID;
        $req->refund_seqno = date("YmdHis");;
        $req->refund_time = date("YmdHis");;
        $req->txn_seqno = "202111081131043299";;
        $req->refund_reason = "reason";
        $req->refund_amount = "0.1";
        $req->notify_url = "http://localhost:9090/notify";
        $req->refund_details = array($refundDetailVo);

        $resp = httpPost(TEST_URL . '/v1/ipay/cashin/refund/apply', json_encode($req));
        Logger()->info($resp);
        $ret_code = (array)json_decode($resp, true)['ret_code'];
        $this->assertSame("0000", $ret_code[0]);
    }

    /**
     * 支付申请
     * @return void
     */
    function test_wechat_ali_pay_apply()
    {
        $goodsInfoVo = new GoodsInfoVo();
        $goodsInfoVo->goods_name = "test goods name";
        $share_info = new ShareInfoVo();
        $shareListVo = new ShareListVo();
        $shareListVo->share_uid = "201608110000196145";
        $shareListVo->share_amount = "1";
        $shareListVo->share_memo = "分账描述";
        $share_info->share_list = array($shareListVo);

        $req = new PayApplyReqVo();
        $req->mch_id = MCH_ID;
        $req->user_id = "202112021358109530";
        $req->txn_seqno = date("YmdHis");
        $req->txn_time = date("YmdHis");
        $req->order_amount = "5";
        $req->goods_info = $goodsInfoVo;
        $req->user_ip = "10.20.232.58";
        $req->order_info = "test order info";
        $req->pay_expire = "600";
        $req->share_info = $share_info;
        $req->pay_type = "WX_SCAN"; // 参考支持的支付方式
        $req->risk_item = "{\"frms_ware_category\":\"2009\",\"user_info_mercht_userno\":\"12345\",\"user_info_bind_phone\":\"13812345678\",\"user_info_dt_register\":\"20141015165530\",\"user_info_full_name\":\"张三丰\",\"user_info_id_no\":\"3306821990012121221\",\"user_info_identify_type\":\"1\",\"user_info_identify_state\":\"1\",\"user_info_id_type\":\"0\",\"frms_client_chnl\":\"10\",\"frms_ip_addr\":\"60.191.76.226\"}";
        $req->extend_info = "{\"appid\":\"wx24f0a46b15545374\",\"openid\":\"oUpF8uNC0x27mr4fkcf2p0Hm1vIQ\"}";
        $req->notify_url = "http://localhost:9090/notify";

        $resp = httpPost(TEST_URL . '/v1/ipay/cashin/wechatali/apply', json_encode($req));
        Logger()->info($resp);
        $ret_code = (array)json_decode($resp, true)['ret_code'];
        $this->assertSame("0000", $ret_code[0]);
    }


    /**
     * 文件上传
     * @return void
     */
    function test_upload_file()
    {
        $e = new UploadDocumentRequest();
        $e->user_no = "1234";
        $e->txn_seqno = date("YmdHis");
        $e->txn_time = date("YmdHis");
        $e->file_type = "png";
        $e->context_type = "UBO_IMAGE";
        $e->file_context = "bb"; // base64处理文件

        $resp = httpPost(FILE_URL . '/merchant/v1/file/uploadfile', json_encode($e));
        $ret_code = (array)json_decode($resp, true)['ret_code'];
        $this->assertSame("0000", $ret_code[0]);

    }


    /**
     * 付款查询
     * @return void
     */
    function test_payment_query()
    {
        $req = new PaymentQueryReqVo();
        $req->mch_id = MCH_ID;
        $req->txn_seqno = date("YmdHis"); // 替换交易单号
        $resp = httpPost(TEST_QUERY_URL . '/v1/ipay/cashout/payment/query', json_encode($req));
        $ret_code = (array)json_decode($resp, true)['ret_code'];
        $this->assertSame("0000", $ret_code[0]);
    }

    /**
     * 退款查询
     * @return void
     */
    function test_refund_query()
    {
        $req = new RefundQueryReqVo();
        $req->mch_id = MCH_ID;
        $req->txn_seqno = date("YmdHis"); // 替换交易单号
        $req->refund_seqno = date("YmdHis"); // 替换退款单号

        $resp = httpPost(TEST_QUERY_URL . '/v1/ipay/cashin/refund/query', json_encode($req));
        $ret_code = (array)json_decode($resp, true)['ret_code'];
        $this->assertSame("0000", $ret_code[0]);
    }

    /**
     * 支付结果查询
     * @return void
     */
    function test_wechat_ali_pay_query()
    {
        $req = new PayQueryReqVo();
        $req->mch_id = MCH_ID;
        $req->txn_seqno = date("YmdHis"); // 替换交易单号

        $resp = httpPost(TEST_QUERY_URL . '/v1/ipay/cashin/pay/query', json_encode($req));
        $ret_code = (array)json_decode($resp, true)['ret_code'];
        $this->assertSame("0000", $ret_code[0]);
    }

    /**
     * 进件结果查询
     * @return void
     */
    function test_register_query()
    {
        $req = new RegisterQueryReqVo();
        $req->txn_seqno ='LLPAY2022022520270199926'; // date("YmdHis"); // 替换交易单号
        $resp = httpPost(TEST_URL . '/v1/customer/access/query', json_encode($req)); // 注意地址为 TEST_URL
        $ret_code = (array)json_decode($resp, true)['ret_code'];
        $this->assertSame("0000", $ret_code[0]);
    }


    /**
     * 签名
     * @return void
     */
    function test_sign()
    {
        $data = "data";
        $sign_data = sign_data($data);
        Logger()->info($sign_data);
    }


    /**
     * 数据加密
     * @return void
     */
    function test_enc()
    {
        $data = "data";
        $rsa = new Rsa(private_key_path, public_key_path);
        $encode_data = $rsa->publicEncrypt($data);
        Logger()->info($encode_data);
        $source = $rsa->privateDecrypt($encode_data);
        Logger()->info($source);
        $this->assertSame($data, $source);
    }
}